import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;
import pluginUtils.CircleArrangement;
import pluginUtils.Complexe; 
import pluginUtils.PackCircle;

public class GenericDrawerPentagon extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	private PrintStream output;
	private double xShift=0; 
	private double zShift=0; 
	
	

	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Green","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	static private int index=0;
	static private boolean nouveau=true; 
	
	private void drawCircle(PackCircle c){
		double xCenter=c.getX(); 
		double yCenter=c.getY(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)((xCenter-xShift)*acoef+ccoef);
		int intY=(int)((yCenter-zShift)*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
		
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
		
		String s="sphere{<"+xCenter+","+yCenter+",0>,";
		s+=c.getRadius(); 
		s+=" texture{pigment{color rgb <"+(1-3*c.getCouleur())+","+(1-2*c.getCouleur())+","+(1-0.8*c.getCouleur())+">}} finish{reflection 0.5}}"; 
		
		String es=("me=translate(["+c.getX()+","+c.getY()+",0],"+c.getRadius()*2.1+")\n");
		if(nouveau){
			nouveau=false; 
			es+="ob=scene.objects.new(me,'sphere"+index+"')";
		}
		else{
			es+="localOb=scene.objects.new(me,'sphere"+index+"')\n"; 
			es+="ob.join([localOb])\n";	
			es+="scene.objects.unlink(localOb)";
		}
		//output.println(es); 
		System.out.println(es); 
		index++; 
		
		
		
		
		
		
	}
	private void drawPoint(Complexe c){
		double xCenter=c.getReal(); 
		double yCenter=c.getIm(); 
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
		
	
	
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		double limit=20; 
	
		setBounds(-limit,limit,-limit,limit); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(3); 
		

		setProcessor("Verifying", primeIP);
		
		drawLine(x0,0,x1,0);
		drawLine(0,y0,0,y1);
		CircleArrangement A=new CircleArrangement();
		A.initPentagonWithRandom(); 
		A.verify(A.getCircleList());
		for(int i=0;i<100000;i++){
			double u=A.iterer(); 
			if(i%10000==0){
				System.out.println(i+" "+u);
			
			
		}
		}
		A.computePosition(); 
		/*
		try{
			output=new PrintStream("f:/Povray/circlepacking.txt");
			}
			catch(Exception e){System.out.println("Probleme "+e); System.exit(0);}
			*/
		for(int i=0;i<PackCircle.size();i++){	
			System.out.println(PackCircle.getCircleAt(i));
			drawCircle(PackCircle.getCircleAt(i));
		}
		
		show();
		System.out.println("done "); 
		//output.close(); 
	}	
}
	